<?php
class View extends Object {
	
	function __construct(&$controller) {
		$this->controller = $controller;
	}
	
	function buttonSet($buttons, $order = 'vertical', $defaultButtonType = 'GtkButton') {		
		switch($order) {
			case 'horizontal':
				$box = new GtkHButtonBox;
				break;
			
			case 'vertical':
				$box = new GtkVButtonBox;
				break;
			
			default:
				return false;
		}
		
		$buttonWidgets = $this->getButtonWidgets($buttons, $defaultButtonType);
		
		foreach($buttonWidgets as $n => $widget) {
			$box->pack_start($widget);
		}

		return $box;
	}
	
	function getButtonWidgets($buttons, $defaultButtonType = 'GtkButton') {
		$buttonWidgets = array();
		foreach($buttons as $button => $settings) {		
			if($button === 0) {
				$button = $settings;
				$settings = array('clicked' => 'action_' . $button);
			} elseif(is_string($settings)) {
				$settings = array('clicked' => $settings);				
			}
			
			if(!isset($settings['buttonType'])) {
				$settings['buttonType'] = $defaultButtonType;
			}
			
			$btn = new $settings['buttonType']($button);
			
			foreach($settings as $key => $setting) {
				if(in_array($key, $this->controller->connectSimpleActions)) {
					if(is_array($setting)) {
						$method = array_shift($setting);
						$btn->connect_simple($key, array($this->controller, $method), $setting);
					} else {
						$btn->connect_simple($key, array($this->controller, $setting));
					}
				}
			}
			$buttonWidgets[] = $btn;
		}
		return $buttonWidgets;
	}
}
?>